package com.unittec.zk.provider.excel.utils;

import com.unittec.zk.provider.entity.ImportTaskEntity;
import com.unittec.zk.provider.excel.service.IImportConsumer;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ci.ren
 */
public interface IExcelImportConsumer<T extends ExcelRow> extends IImportConsumer<T> {

    /**
     * 实现这个方法，方法里面不要调用有事务的方法
     *
     * @param dataList
     * @param importTask
     * @param paramMap
     */
    void process(List<T> dataList, ImportTaskEntity importTask, Map<String, Object> paramMap);

    /**
     * 导入业务方法
     * 
     * @param dataList
     * @param importTask
     * @param paramMap
     * @return
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    default int consume(List<T> dataList, ImportTaskEntity importTask, Map<String, Object> paramMap) {
        this.process(dataList, importTask, paramMap);
        long successful = dataList.stream().filter(e -> e.getErrorMessage() == null).collect(Collectors.counting());
        importTask.setSuccessful(importTask.getSuccessful() + (int)successful);
        return (int)successful;
    }
}
